<template>
  <div>
    <el-form :inline="true" :model="searchModel" class="demo-form-inline">
      <el-form-item label="资质名称">
        <el-input
          v-model="searchModel.searchItem.name"
          placeholder="资质名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="级别">
        <dictionaryTableSelect
          v-model="searchModel.searchItem.levelid"
          typeKey="leavl"
          :fieldValue.sync="customerInfo.c_type"
        ></dictionaryTableSelect>
      </el-form-item>
      <el-form-item label="年审时间">
        <el-date-picker
          v-model="searchModel.searchItem.reviewdate"
          type="date"
          placeholder="选择日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="SearchPage">查询</el-button>
        <el-button type="primary" @click="openAdd">添加</el-button>
        <el-button type="danger" @click="atsDelete">删除</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableData.data" style="width: 100%" ref="cbk">
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="qua_manager_name" label="名称"> </el-table-column>
      <el-table-column prop="qua_begin_date" label="资质开始时间">
      </el-table-column>
      <el-table-column prop="qua_end_date" label="资质开结束时间">
      </el-table-column>
      <el-table-column prop="qua_review_date" label="年审时间">
      </el-table-column>
      <el-table-column prop="qua_manager_content" label="内容">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="LookEdit(scope.$index, scope.row.qua_manager_id)"
            >查看</el-button
          >
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row.qua_manager_id)"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="searchModel.pageIndex"
      :page-sizes="[1, 3, 5, 10]"
      :page-size="searchModel.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="tableData.total"
    >
    </el-pagination>

    <el-dialog
      title="资质添加"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <el-form ref="form" :model="addinfo" label-width="80px">
        <el-form-item label="名称">
          <el-input v-model="addinfo.qua_manager_name"></el-input>
        </el-form-item>
        <el-form-item label="资质开始时间">
          <el-input type="date" v-model="addinfo.qua_begin_date"></el-input>
        </el-form-item>
        <el-form-item label="资质开结束时间">
          <el-input type="date" v-model="addinfo.qua_end_date"></el-input>
        </el-form-item>
        <el-form-item label="年审时间">
          <el-input type="date" v-model="addinfo.qua_review_date"></el-input>
        </el-form-item>
        <el-form-item label="级别">
          <dictionaryTableSelect
            v-model="addinfo.qua_manager_level"
            typeKey="leavl"
            :fieldValue.sync="customerInfo.c_typeadd"
          ></dictionaryTableSelect>
        </el-form-item>
        <el-form-item label="内容">
          <el-input v-model="addinfo.qua_manager_content"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmitAdd">立即创建</el-button>
          <el-button @click="dialogVisible = false">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-dialog
      title="资质编辑"
      :visible.sync="dialogVisibles"
      width="30%"
      :before-close="handleClose"
    >
      <el-form ref="form" :model="addinfos" label-width="80px">
        <el-form-item label="名称">
          <el-input v-model="addinfos.qua_manager_name"></el-input>
        </el-form-item>
        <el-form-item label="资质开始时间">
          <el-input type="datetime" v-model="addinfos.qua_begin_date"></el-input>
        </el-form-item>
        <el-form-item label="资质开结束时间">
          <el-input type="datetime" v-model="addinfos.qua_end_date"></el-input>
        </el-form-item>
        <el-form-item label="年审时间">
          <el-input type="datetime" v-model="addinfos.qua_review_date"></el-input>
        </el-form-item>
        <el-form-item label="级别">
          <dictionaryTableSelect
            v-model="addinfos.qua_manager_level"
            typeKey="leavl"
            :fieldValue.sync="customerInfo.c_typeadd"
          ></dictionaryTableSelect>
        </el-form-item>
        <el-form-item label="内容">
          <el-input v-model="addinfos.qua_manager_content"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmitUpdate">立即修改</el-button>
          <el-button @click="dialogVisibles = false">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-dialog
      title="资质查看"
      :visible.sync="dialogVisibless"
      width="30%"
      :before-close="handleClose"
    >
      <el-form ref="form" :model="addinfos" label-width="80px">
        <el-form-item label="名称">
          {{ addinfos.qua_manager_name }}
        </el-form-item>
        <el-form-item label="资质开始时间">
          {{ addinfos.qua_begin_date }}
        </el-form-item>
        <el-form-item label="资质开结束时间">
          {{ addinfos.qua_end_date }}
        </el-form-item>
        <el-form-item label="年审时间">
          {{ addinfos.qua_review_date }}
        </el-form-item>
        <el-form-item label="级别">
          {{ addinfos.qua_manager_level }}
        </el-form-item>
        <el-form-item label="内容">
          {{ addinfos.qua_manager_content }}
        </el-form-item>
        <el-form-item>
          <el-button @click="dialogVisibless = false">返 回</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

  <script>
import {
  QuaAdd,
  QuaDelete,
  QuaEdit,
  QuaUpdate,
  SearchPage,
} from "./QualificationManagementApi";
export default {
  data() {
    return {
      uploadUrl:
        process.env.VUE_APP_BASE_API + "FileManager/FileManager/FilesUpload",
      customerInfo: {
        fileList: [],
        c_type: null,
        c_typeadd: null,
      },
      typeInfo: {},
      dialogVisible: false,
      addinfo: {
        qua_manager_level: null,
      },
      dialogVisibles: false,
      addinfos: {
        qua_manager_id: null,
      },
      dialogVisibless: false,
      tableData: [],
      searchModel: {
        searchItem: {
          name: null,
          levelid: null,
          reviewdate: null,
        },
        pageIndex: 1,
        pageSize: 3,
      },
    };
  },
  created() {
    this.SearchPage();
  },
  methods: {
    onSubmitUpdate() {
      QuaUpdate(this.addinfos).then((r) => {
        if (r.data) {
          this.$message("修改成功!");
          this.dialogVisibles = false;
          this.SearchPage();
        }
      });
    },
    LookEdit(index, row) {
      this.dialogVisibless = true;
      QuaEdit(row).then((r) => {
        this.addinfos = r.data;
        this.customerInfo.c_typeadd = r.data.qua_manager_level;
      });
    },
    handleEdit(index, row) {
      this.addinfos.qua_manager_id = row;
      this.dialogVisibles = true;
      QuaEdit(row).then((r) => {
        this.addinfos = r.data;
         this.customerInfo.c_typeadd = r.data.qua_manager_level;
      });
    },
    openAdd() {
      this.addinfo = {};
      this.dialogVisible = true;
    },
    onSubmitAdd() {
      this.addinfo.qua_manager_level = this.customerInfo.c_typeadd;
      QuaAdd(this.addinfo).then((r) => {
        if (r.data) {
          console.log(r);
          this.$message("添加成功!");
          this.dialogVisible = false;
          this.SearchPage();
        }
      });
    },
    SearchPage() {
      this.searchModel.searchItem.levelid = this.customerInfo.c_type;
      SearchPage(this.searchModel).then((r) => {
        console.log(this.searchModel);
        console.log(r.data.data);
        this.tableData = r.data;
      });
    },
    handleSizeChange(val) {
      this.searchModel.pageSize = val;
      this.searchModel.pageIndex = 1;
      this.SearchPage();
    },
    handleCurrentChange(val) {
      this.searchModel.pageIndex = val;
      this.SearchPage();
    },
    atsDelete() {
      var arr = this.$refs.cbk.selection;
      if (arr.length == 0) {
        this.$message("请选择至少一条数据");
        return;
      }
      var ids = new Array();
      arr.forEach((obj) => {
        ids.push(obj.qua_manager_id);
      });
      if (confirm("确认要删除吗？")) {
        QuaDelete(ids).then((r) => {
          if (r.data) {
            this.$message("删除成功");
            this.SearchPage();
          }
        });
      }
    },
  },
  components: {
    dictionaryTableSelect: () =>
      import("@/components/system_manager/DictionaryTableSelect"),
  },
};
</script>